<template>
    <div class="tian">
        <div class="top">
            <div class="box">
                <Tickets />
            </div>
            <span style="margin-left: 10px;">数据列表</span>
        </div>
        <div class="bottom">
            <el-row class="mb-4">
                <el-button @click="Add">添加</el-button>
            </el-row>
        </div>
    </div>
    <!-- 表格 -->
    <div class="table-container">
        <el-table :data="Flash" border style="width: 100%" table-layout="auto">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="id" label="编号" />
            <el-table-column prop="name" label="原因类型" />
            <el-table-column prop="sort" label="排序" />
            <el-table-column label="是否启用">
                <template #default="{ row }">
                    <el-switch v-model="row.status" :active-value="1" :inactive-value="0" />
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="添加时间">
                <template #default="scope">
                    {{ dayjs(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
                <template #default="{ row }">
                    <el-button link type="primary" size="small" @click="compile(row.id)">编辑</el-button>
                    <el-button link type="primary" size="small" @click="remove(row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <div class="formm">
        <span class="form">
            <el-form-item label="">
                <el-select v-model="form.region" placeholder="批量操作" clearable>
                    <el-option label="删除" value="shanghai" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary">确认</el-button>
            </el-form-item>
        </span>

        <span class="fen">
            <el-pagination v-model:current-page="paginationParams.currentPage" v-model:page-size="paginationParams.pageSize"
                :page-sizes="[5, 10, 20]" layout="total, sizes, prev, pager, next, jumper" :total="paginationParams.total"
                :background="true" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </span>
    </div>
    <!-- 添加或编辑 -->
    <AddOrEdit :id="activityId" @cancel="abolish" @confirm="affirm" :dialogFormVisible="dialogFormVisible" />
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import * as http from '@/request/http'
import { ElMessage, ElMessageBox, dayjs } from 'element-plus'
import AddOrEdit from './AddOrEdit.vue'
const paginationParams = reactive({
    currentPage: 1,
    pageSize: 5,
    total: 100,

})

const Flash = ref([])
const refresh = () => {
    http.reReas.returnReasonList({
        pageNum: paginationParams.currentPage,
        pageSize: paginationParams.pageSize,
    }).then(res => {
        console.log(res);
        Flash.value = res.data.list as any
        paginationParams.total = res.data.total as any
    })
}
refresh()

const handleSizeChange = (_val: number) => {
    // console.log("页容量发生变化触发", `${val} items per page`)
    //重新请求
    refresh()
}
const handleCurrentChange = (_val: number) => {
    // console.log("页码发生变化", `current page: ${val}`)
    //重新请求
    refresh()
}

const form = reactive({
    region: '',
    id: 0,
    
})

const dialogFormVisible = ref(false);
const activityId = ref(0)
const Add = () => { //点击添加按钮
    activityId.value = 0
    dialogFormVisible.value = true
}

const abolish = () => {
    dialogFormVisible.value = false
}
const affirm = () => {
    dialogFormVisible.value = false
    refresh() //重新更新
}

const compile = (id: number) => { //点击编辑按钮

    activityId.value = id//点击 编辑时 修改当前活动id为传进来的参数id(当前编辑的活动id)
    dialogFormVisible.value = true;   // 打开 编辑窗口

}

//删除
const remove = (id: number) => {
    // const idn = [id]
    ElMessageBox.confirm(
        '是否要删除该活动?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => { //删除
        //发起请求，删除活动
        http.reReas.returnReasonDelete(id).then(res => {//判断是否成功删除活动
            if (res.code === 200) {
                ElMessage.success("成功删除")
                refresh()
            }
        })
    }).catch(() => {
        ElMessage.warning('取消删除')
    })

}
</script>

<style scoped>
.top {
    display: flex;
}

.box {
    width: 15px;
    height: 15px;

}

.tian {
    display: flex;
    justify-content: space-between;

    border: 1px solid gainsboro;
    height: 50px;
    line-height: 50px;
    margin: 20px;
    border-radius: 10px;
    padding: 10px;
}

.bottom {
    margin-top: 10px;
}

.formm {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
}

.form {
    display: flex;
}
</style>